package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PublicNoticeService {

	@Autowired
	private PublicNoticeDao publicNoticeDao;

	private QPublicNotice $ = QPublicNotice.publicNotice;

	@Autowired
	public void setPublicNoticeDao(PublicNoticeDao publicNoticeDao) {
		this.publicNoticeDao = publicNoticeDao;
	}

	public List<PublicNotice> findAll() {
		return ListUtil.toList(publicNoticeDao.findAll());
	}

	public List<PublicNotice> findAll(String title, String content,
			Date publishTimeBegin, Date publishTimeEnd, Long publisherId,
			String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (StringUtil.isNotEmpty(title)) {
			condition.and($.title.contains(title));
		}

		if (StringUtil.isNotEmpty(content)) {
			condition.and($.content.contains(content));
		}

		if (publishTimeBegin != null) {
			condition.and($.publishTime.after(publishTimeBegin));
		}

		if (publishTimeEnd != null) {
			condition.and($.publishTime.before(publishTimeEnd));
		}

		if (IdUtil.isNotEmpty(publisherId)) {
			condition.and($.publisher.id.eq(publisherId));
		}

		return ListUtil.toList(publicNoticeDao.findAll(condition));
	}

	public Page<PublicNotice> findAll(String title, String content,
			Date publishTimeBegin, Date publishTimeEnd, Long publisherId,
			int pageNumber, int pageSize, String sortAttribute,
			String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (StringUtil.isNotEmpty(title)) {
			condition.and($.title.contains(title));
		}
		if (StringUtil.isNotEmpty(content)) {
			condition.and($.content.contains(content));
		}
		if (publishTimeBegin != null) {
			condition.and($.publishTime.after(publishTimeBegin));
		}

		if (publishTimeEnd != null) {
			condition.and($.publishTime.before(publishTimeEnd));
		}
		if (IdUtil.isNotEmpty(publisherId)) {
			condition.and($.publisher.id.eq(publisherId));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return publicNoticeDao.findAll(condition, pageRequest);
	}

	public PublicNotice getPublicNotice(Long id) {
		return publicNoticeDao.getOne(id);
	}

	public void deletePublicNotice(Long id) {
		publicNoticeDao.delete(id);
	}

	public void savePublicNotice(PublicNotice publicNotice) {
		if(null == publicNotice.getPublishTime()){
			publicNotice.setPublishTime(DateUtil.nowDate());
		}
		publicNoticeDao.save(publicNotice);
	}

	public PublicNotice findOne(Long id) {
		return this.publicNoticeDao.findOne(id);
	}

	public List<PublicNotice> findNew() {
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "id");
		PageRequest pageRequest = new PageRequest(0, 5, sortObject);
		Page<PublicNotice> pages = publicNoticeDao.findAll(condition, pageRequest);
		return pages.getContent();
	}

}
